package com.hbnu.service.impl;

import com.hbnu.dao.UserDao;
import com.hbnu.dao.impl.UserDaoImpl;
import com.hbnu.pojo.DormBuild;
import com.hbnu.pojo.User;
import com.hbnu.service.UserService;
import com.hbnu.util.PageModel;

import java.util.List;

/**
 * @author chendikai
 * @date 2024-11-06 上午 10:37
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();
    private DormBuildDao dormBuildDao = new DormBuildDaoImpl();

    @Override
    public List<User> findManager(String searchType, String keyword) {
        List<User> users = userDao.findManager(searchType, keyword);

        for (User user : users) {
            List<DormBuild> builds = dormBuildDao.findByUserId(user.getId());
            user.setDormBuilds(builds);
        }
        return users;
    }
    @Override
    public List<User> findStudent(String dormBuildId, String searchType, String
            keyword, User user, PageModel pageModel) {
        StringBuffer sql = new StringBuffer();
        // 不管当前用户角色是怎么样，查询的都是学生，所有role_id=2
        /*sql.append("SELECT * FROM tb_user  user WHERE user.role_id = 2 ");*/
        sql.append("SELECT user.*,build.name buildName,build.*  FROM tb_user user " + " LEFT JOIN tb_dormbuild  build ON build.id = user.dormBuildId " + " where user.role_id = 2");
        if (keyword != null && !keyword.equals("") &&
                "name".equals(searchType)) {
            // 根据名字查询
            sql.append("  and  user.name like '%" + keyword.trim() + "%'");
        } else if (keyword != null && !keyword.equals("") &&
                "stuCode".equals(searchType)) {
            // 根据学号查询
            sql.append(" and user.stu_code = '" + keyword.trim() + "'");
        } else if (keyword != null && !keyword.equals("") &&
                "dormCode".equals(searchType)) {
            // 根据宿舍编号查询
            sql.append(" and user.dorm_code = '" + keyword.trim() + "'");
        } else if (keyword != null && !keyword.equals("") &&
                "sex".equals(searchType)) {
            // 根据性别查询
            sql.append(" and user.sex = '" + keyword.trim() + "'");
        } else if (keyword != null && !keyword.equals("") &&
                "tel".equals(searchType)) {
            // 根据电话号码查询
            sql.append(" and user.tel = '" + keyword.trim() + "'");
        }
        if (dormBuildId != null && !dormBuildId.equals("")) {
            // 根据宿舍楼id查询
            sql.append(" and user.dormBuildId = '" + dormBuildId + "'");
        }
        /*sql.append(" and user.dormBuildId in (2,3,4)")*/
        // 判断当前用户是否为宿舍管理员，如是则需查询其管理的所有宿舍楼id
        if (user.getRoleId().equals(1)) {
            // 获取当前宿舍管理员管理的所有宿舍楼
            List<DormBuild> builds = dormBuildDao.findByUserId(user.getId());
            sql.append(" and user.dormBuildId in (");
            for (int i = 0; i < builds.size(); i++) {
                sql.append(builds.get(i).getId() + ",");
            }
            // 删除最后一个,
            sql.deleteCharAt(sql.length() - 1);
            sql.append(")");
        }
        sql.append(" limit " + pageModel.getStartNum() + " , " +
                pageModel.getPageSize());
        List<User> students = userDao.findStudent(sql.toString());
        return students;
    }

    @Override
    public Integer findTotalNum(String dormBuildId, String searchType, String
            keyword, User user) {
        StringBuffer sql = new StringBuffer();
        // 不管当前用户角色是怎么样，查询的都是学生，所有role_id=2
        sql.append("SELECT count(*)  FROM tb_user user " + " LEFT JOIN tb_dormbuild  build ON build.id = user.dormBuildId " + " where user.role_id = 2");
        if (keyword != null && !keyword.equals("") &&
                "name".equals(searchType)) {
            // 根据名字查询
            sql.append("  and  user.name like '%" + keyword.trim() + "%'");
        } else if (keyword != null && !keyword.equals("") &&
                "stuCode".equals(searchType)) {
            // 根据学号查询
            sql.append(" and user.stu_code = '" + keyword.trim() + "'");
        } else if (keyword != null && !keyword.equals("") &&
                "dormCode".equals(searchType)) {
            // 根据宿舍编号查询
            sql.append(" and user.dorm_code = '" + keyword.trim() + "'");
        } else if (keyword != null && !keyword.equals("") &&
                "sex".equals(searchType)) {
            // 根据性别查询
            sql.append(" and user.sex = '" + keyword.trim() + "'");
        } else if (keyword != null && !keyword.equals("") &&
                "tel".equals(searchType)) {
            // 根据电话号码查询
            sql.append(" and user.tel = '" + keyword.trim() + "'");
        }
        if (dormBuildId != null && !dormBuildId.equals("")) {
            // 根据宿舍楼id查询
            sql.append(" and user.dormBuildId = '" + dormBuildId + "'");
        }
        /*sql.append(" and user.dormBuildId in (2,3,4)")*/
        // 判断当前用户是否为宿舍管理员，如是则需查询其管理的所有宿舍楼id
        if (user.getRoleId().equals(1)) {
            // 获取当前宿舍管理员管理的所有宿舍楼
            List<DormBuild> builds = dormBuildDao.findByUserId(user.getId());
            sql.append(" and user.dormBuildId in (");
            for (int i = 0; i < builds.size(); i++) {
                sql.append(builds.get(i).getId() + ",");
            }
            // 删除最后一个,
            sql.deleteCharAt(sql.length() - 1);
            sql.append(")");
        }
        return userDao.findTotalNum(sql.toString());
    }


    @Override
    public User findUserByStuCodeAndPassword(String stuCode, String password) {

        return userDao.findUserByStuCodeAndPassword(stuCode, password);
    }

    @Override
    public List<User> findAllManager(String searchType, String keyword) {

        List<User> userList = userDao.findManager(searchType, keyword);

        // 根据管理员id查询所管理的宿舍楼信息
        if (null != userList) {
            for (User user : userList) {
                Integer userId = user.getId();  // 获取用户id
                List<DormBuild> dormBuilds = userDao.findDormBuildsByUserId(userId);

                user.setDormBuilds(dormBuilds);
            }
        }

        return userList;
    }

    @Override
    public User findByStuCode(String stuCode) {
        return userDao.findByStuCode(stuCode);
    }

    @Override
    public User findById(int id) {
        return userDao.findById(id);
    }

    @Override
    public void updateStudent(User studentUpdate) {
        userDao.updateStudent(studentUpdate);
    }
    @Override
    public void saveStudent(User user) {
        userDao.saveStudent(user);
    }

    @Override
    public User findByUserIdAndManager(Integer id, User user) {
        StringBuffer sql = new StringBuffer();
        sql.append("select * from tb_user  user where user.id =" + id);
        if (user.getRoleId() != null && user.getRoleId().equals(1)) {
            // 表示当前登录的用户角色是宿舍管理员，则要求要修改的学生必须居住在该宿舍管理员管理的宿舍楼中
            List<DormBuild> builds = dormBuildDao.findByUserId(user.getId());
            sql.append(" and user.dormBuildId in (");
            for (int i = 0; i < builds.size(); i++) {
                sql.append(builds.get(i).getId() + ",");// (1,2,)
            }
            // 删除最后一个,
            sql.deleteCharAt(sql.length() - 1);
            sql.append(")");
        }
        return userDao.findByUserIdAndManager(sql.toString());
    }

}
